package com.xxk.rocket.star.core.annotation.impl;

import com.xxk.rocket.star.annotation.StargateNameServer;
import com.xxk.rocket.star.core.annotation.AbsProcessAnnotationProducerAndConsume;
import com.xxk.rocket.star.core.dto.StargateNameServerDTO;
import com.xxk.rocket.star.core.utils.StringUtils;

import java.lang.reflect.Method;

public class StargateNameServerProcessAnnotation
        extends AbsProcessAnnotationProducerAndConsume<StargateNameServerDTO> {

  @Override
  protected void processProducer(StargateNameServerDTO stargateNameServerDTO, Class aClass,
                                 Object o,
                                 Method method) throws Throwable {
    processStargate(stargateNameServerDTO, aClass, method);
  }

  @Override
  protected void processConsume(StargateNameServerDTO stargateNameServerDTO, Class aClass, Object o,
                                Method method) throws Throwable {
    processStargate(stargateNameServerDTO, aClass, method);
  }

  public void processStargate(StargateNameServerDTO stargateNameServerDTO, Class aClass,
                              Method method) throws Throwable {
    StargateNameServer stargateNameServer = null;
    if (method == null) {
      stargateNameServer = (StargateNameServer) aClass.getAnnotation(StargateNameServer.class);
    } else {
      stargateNameServer = method.getAnnotation(StargateNameServer.class);
    }
    if (stargateNameServer == null || StringUtils.isBlank(stargateNameServer.value())) {
      return;
    }
    stargateNameServerDTO.setNamesrvAddr(stargateNameServer.value());
  }
}
